package com.university.trade.util;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.GlobalHistogramBinarizer;

/**
 * 二维码加密解密工具类
 * 
 * @author yaojinshan
 * @since 2016-11-22 12:56:04
 * @filename TwoDimensionCodeUtils.java
 */
public class QRCodeUtils {
	private static int imageWidth = 250;//图片宽度
	private static int imageHeight = 250;//图片高度
	/**
	 * 生成二维码
	 * @param content 内容
	 * @param targetDir 图片保存路径
	 * @param fileName 文件名
	 * @param charset 编码集
	 * @param imageFormat 图片格式
	 * @param width 图片宽度
	 * @param height 图片高度
	 * @throws WriterException
	 * @throws IOException
	 */
	public static void createQRCode(String content,String targetDir,String fileName,String charset,String imageFormat,int width,int height) throws WriterException, IOException {
		com.google.zxing.Writer writer = new MultiFormatWriter();
		Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(2);
		hints.put(EncodeHintType.CHARACTER_SET, charset);
		if(0 == width || 0 == height){
			height = imageHeight;
			width = imageWidth;
		}
		BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
		File imagePath = new File(targetDir + fileName + "." + imageFormat);
		MatrixToImageWriter.writeToFile(matrix, imageFormat, imagePath);
	}
	/**
	 * 二维码解密
	 * 
	 * @throws IOException
	 * @throws NotFoundException
	 * @throws ChecksumException
	 * @throws FormatException
	 */
	public static void decoderQRCode(String targetDir,String fileName,String charset,String imageFormat) throws IOException, FormatException, NotFoundException, ChecksumException {
		File imagePath = new File(targetDir + File.separator + fileName + "." + imageFormat);
		Reader reader = new MultiFormatReader();
		LuminanceSource ls = new BufferedImageLuminanceSource(ImageIO.read(imagePath));
		Binarizer b = new GlobalHistogramBinarizer(ls);
		Hashtable<DecodeHintType, String> hint1 = new Hashtable<DecodeHintType, String>(2);
		hint1.put(DecodeHintType.CHARACTER_SET, charset);
		Result r = reader.decode(new BinaryBitmap(b), hint1);
		System.out.println(r.getText());
	}
	
	public static void main(String[] args) throws Exception {
		//encoderQRCode();
		//decoderQRCode();
		createQRCode("www.baidu.com","d:/",String.valueOf(new Date().getTime()),"UTF-8","png",450,450);
	}
	/**
	 * 二维码解密 测试方法
	 * 
	 * @throws IOException
	 * @throws NotFoundException
	 * @throws ChecksumException
	 * @throws FormatException
	 */
	public static void decoderQRCode() throws IOException, FormatException, NotFoundException, ChecksumException {
		File imagePath = new File("d:/test.jpg");
		String encoding = "utf-8";
		Reader reader = new MultiFormatReader();
		LuminanceSource ls = new BufferedImageLuminanceSource(ImageIO.read(imagePath));
		Binarizer b = new GlobalHistogramBinarizer(ls);
		Hashtable<DecodeHintType, String> hint1 = new Hashtable<DecodeHintType, String>(2);
		hint1.put(DecodeHintType.CHARACTER_SET, encoding);
		Result r = reader.decode(new BinaryBitmap(b), hint1);
		System.out.println(r.getText());
	}
	/**
	 * 二维码加密 测试方法
	 * 
	 * @throws IOException
	 * @throws NotFoundException
	 * @throws ChecksumException
	 * @throws FormatException
	 * @throws WriterException 
	 */
	public static void encoderQRCode() throws IOException, FormatException, WriterException {
		String content = "www.baidu.com";
		String encoding = "utf-8";
		String format = "jpg";
		File imagePath = new File("d:/test.jpg");
		com.google.zxing.Writer writer = new MultiFormatWriter();
		Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(2);
		hints.put(EncodeHintType.CHARACTER_SET, encoding);
		BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, imageWidth, imageHeight, hints);
		MatrixToImageWriter.writeToFile(matrix, format, imagePath);
	}

}
